<!--
 * @Date: 2021-12-20 15:37:21
 * @LastEditors: LeeKing
 * @LastEditTime: 2021-12-22 10:14:07
 * @FilePath: \hrsaas\src\views\employees\components\add-employees.vue
-->
<template>
  <el-dialog title="新增员工" :visible="visible" width="40%" @close="onClose">
    <el-form ref="form" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="form.username"
          style="width: 80%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="form.mobile"
          style="width: 80%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          style="width: 80%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="form.formOfEmployment"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          /></el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="form.workNumber"
          style="width: 80%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          style="width: 80%"
          placeholder="请选择部门"
          @focus="onDepartmentFocus"
        />
        <el-tree
          v-if="isShowTree"
          v-loading="loading"
          :data="treeData"
          :props="{ label: 'name' }"
          @node-click="onTreeNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          style="width: 80%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartmentsList } from '@/api/departments.js'
import { addEmployees } from '@/api/employees.js'
import { transListToTree } from '@/utils'
import employees from '@/api/constant/employees.js'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        correctionTime: '',
        timeOfEntry: '',
        departmentName: '',
        workNumber: '',
        formOfEmployment: '',
        mobile: '',
        username: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          {
            required: true,
            message: '部门不能为空',
            trigger: ['change', 'blur']
          }
        ],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
        correctionTime: [
          { required: true, message: '转正时间', trigger: 'blur' }
        ]
      },
      treeData: [],
      isShowTree: false,
      loading: false,
      hireType: employees.hireType
    }
  },

  created() {},

  methods: {
    // 关闭弹窗
    onClose() {
      this.$emit('update:visible', false)
      this.$refs.form.resetFields()
      this.isShowTree = false
    },
    async onDepartmentFocus() {
      this.loading = true
      this.isShowTree = true
      const res = await getDepartmentsList()
      console.log(res)
      this.treeData = transListToTree(res.depts, '')
      this.loading = false
    },
    // 节点被点击时的回调
    onTreeNode(data) {
      this.form.departmentName = data.name
      this.isShowTree = false
    },
    async onSave() {
      try {
        await this.$refs.form.visible
        await addEmployees(this.form)
        this.$message.success('新增员工成功!')
        // this.$emit('update:visible', false)
        // this.$emit('add-success')
        // 单个父 直接调用父
        this.$parent.loadEmployeesList()
        this.$parent.addEmployeesDialogVisible = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
